<?php

require_once dirname(__FILE__) . '/../lib/pagesGeneratorConfiguration.class.php';
require_once dirname(__FILE__) . '/../lib/pagesGeneratorHelper.class.php';

/**
 * pages actions.
 *
 * @package    dolpheuscms
 * @subpackage pages
 * @author     Harrie Essing
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class pagesActions extends autoPagesActions {

    public function executeListUp(sfWebRequest $request) {
        $pages = $this->getRoute()->getObject();

        if ($pages->getNode()->hasPrevSibling()) {
            $currentSibling = $pages->getTitle();
            $prevSibling = $pages->getNode()->getPrevSibling();

            $pages->getNode()->moveAsPrevSiblingOf($prevSibling);

            $this->getUser()->setFlash('notice', $pages->getTitle() . ' positie omhoog');
        } else {
            $this->getUser()->setFlash('error', $pages->getTitle() . ' kan geen positie omhoog');
        }

        $this->redirect('@pages');
    }

    public function executeListDown(sfWebRequest $request) {
        $pages = $this->getRoute()->getObject();

        if ($pages->getNode()->hasNextSibling()) {
            $currentSibling = $pages->getTitle();
            $nextSibling = $pages->getNode()->getNextSibling();

            $pages->getNode()->moveAsNextSiblingOf($nextSibling);

            $this->getUser()->setFlash('notice', $pages->getTitle() . ' positie omlaag');
        } else {
            $this->getUser()->setFlash('error', $pages->getTitle() . ' kan geen positie omlaag');
        }

        $this->redirect('@pages');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->dispatcher->notify(new sfEvent($this, 'admin.delete_object', array('object' => $this->getRoute()->getObject())));

        if ($this->getRoute()->getObject()->getNode()->delete()) {
            $this->getUser()->setFlash('notice', 'The item was deleted successfully.');
        }

        $this->redirect('@pages');
    }

    protected function executeBatchDelete(sfWebRequest $request) {
        $ids = $request->getParameter('ids');
        $records = Doctrine_Query::create()
                        ->from('Pages')
                        ->whereIn('id', $ids)
                        ->execute();
        foreach ($records as $record) {
            $record->getNode()->delete();
        }

        $this->getUser()->setFlash('notice', 'The selected items have been deleted successfully.');
        $this->redirect('@pages');
    }

    protected function addSortQuery($query) {
        $query->addOrderBy('lft asc');
        $query->where('level > 0');
    }

}
